//
//  CustomAlertView.m
//  Elife
//
//  Created by 拓客营 on 2022/4/14.
//

#import "CustomAlertView.h"

@interface ShowCustomAlertView ()

@end

@implementation ShowCustomAlertView

+(void)showCustomAlertWithTitle:(NSString *)title leftTitle:(nonnull NSString *)leftTitle rightTitle:(nonnull NSString *)rightTitle leftAction:(nullable void (^)(void))leftActionBlock rightAction:(nullable void (^)(void))rightActionBlock {
    CustomAlertView *view = [[CustomAlertView alloc]initWithTitle:title leftTitle:leftTitle rightTitle:rightTitle];
    view.tag = 808080808;
    if (leftActionBlock) {
        view.customAlertLeftActionBlock = ^{
            leftActionBlock();
        };
    }
    if (rightActionBlock) {
        view.customAlertRightActionBlock = ^{
            rightActionBlock();
        };
    }
    
    view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [kWindow addSubview:view];
}

+(void)hiddenAlertView {
    CustomAlertView *view = [kWindow viewWithTag:808080808];
    [view removeFromSuperview];
}

@end

#pragma mark ----------------------------

@interface CustomAlertView()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *titleLab;


@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;

@property (nonatomic, copy) NSString *titleStr;
@property (nonatomic, copy) NSString *leftStr;
@property (nonatomic, copy) NSString *rightStr;

@end

@implementation CustomAlertView

-(instancetype)initWithTitle:(NSString *)title leftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle {
    if (self = [super init]) {
        self.titleStr = title;
        self.leftStr = leftTitle;
        self.rightStr = rightTitle;
        [self UIConfig];
    }
    return self;
}

-(void)UIConfig {
    self.backgroundColor = kRGBA(0x000000, 0.5);
    
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.width.mas_equalTo(ScreenWidth-80);
    }];
}

#pragma mark - event
-(void)leftBtnAction {
    if (self.customAlertLeftActionBlock) {
        self.customAlertLeftActionBlock();
    }
    [self removeFromSuperview];
}

-(void)rightBtnAction {
    if (self.customAlertRightActionBlock) {
        self.customAlertRightActionBlock();
    }
    [self removeFromSuperview];
}

#pragma mark - lazy

-(UIView *)contentView {
    if (!_contentView) {
        _contentView = [UIView new];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.cornerRadius = 8;
        _contentView.clipsToBounds = YES;
        
        [_contentView addSubview:self.titleLab];
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(44);
            make.left.mas_equalTo(_contentView.mas_left).offset(25);
            make.right.mas_equalTo(_contentView.mas_right).offset(-25);
        }];
        
        [_contentView addSubview:self.leftBtn];
        [_contentView addSubview:self.rightBtn];
        
        [@[_leftBtn, _rightBtn] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_titleLab.mas_bottom).offset(25);
            make.size.mas_equalTo(CGSizeMake(120, 42));
            make.bottom.mas_equalTo(-50);
        }];
        
        // 分割view
        UIView *view = [UIView new];
        view.backgroundColor = [UIColor clearColor];
        [_contentView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(25, 1));
            make.centerX.mas_equalTo(_contentView);
            make.centerY.mas_equalTo(_leftBtn);
        }];
        [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(view.mas_left);
        }];
        [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(view.mas_right);
        }];
        
    }
    return _contentView;
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.textColor = kRGB(0x333333);
        _titleLab.font = [UIFont systemFontOfSize:16];
        _titleLab.numberOfLines = 0;
        _titleLab.text = _titleStr;
        _titleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLab;
}

-(UIButton *)leftBtn {
    if (!_leftBtn) {
        _leftBtn = [UIButton buttonConfig:_leftStr titleColor:kThemeColor backgroundColor:[UIColor whiteColor] fontSize:16];
        [_leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _leftBtn.layer.cornerRadius = 21;
        _leftBtn.layer.borderColor = kThemeColor.CGColor;
        _leftBtn.layer.borderWidth = 1;
    }
    return _leftBtn;
}

-(UIButton *)rightBtn {
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonConfig:_rightStr titleColor:[UIColor whiteColor] backgroundColor:[UIColor clearColor] fontSize:16];
        [_rightBtn customGradientWithCornerRadius:21 size:CGSizeMake(120, 42)];
        [_rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}
@end
